package com.igeek.web;

import com.igeek.domain.User;
import com.igeek.service.Userservice;
import com.igeek.utils.Mailutils;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.converters.DateConverter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.UUID;

@WebServlet("/user")
public class UserServlet extends BaseServlet {
    public void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        User user=new User();
        try {
            BeanUtils.populate(user,request.getParameterMap());
            Userservice service =new Userservice();
            User u =service.login(user);
            System.out.println(u);
            if (u!=null){
                request.getSession().setAttribute("user",u);
                String url= (String) request.getSession().getAttribute("url");
                if (url==null){
                    request.getRequestDispatcher("index.jsp").forward(request,response);
                }else{
                    request.getSession().removeAttribute("url");
                    response.sendRedirect(url);
                }
            }else{
                response.sendRedirect("login.jsp");
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e){
            e.printStackTrace();
        }
    }
    public void regist(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        DateConverter dateConverter=new DateConverter();
        dateConverter.setPatterns(new String[]{"yyyy-MM-dd","yyyy/MM/dd","yyyy-MM-dd HH:mm:ss"});
        ConvertUtils.register(dateConverter,Date.class);
        User user=new User();
        try {
            BeanUtils.populate(user,request.getParameterMap());
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        String uuid = UUID.randomUUID().toString();
        user.setUid(uuid);
        String activeCode=UUID.randomUUID().toString();
        user.setCode(activeCode);
        Userservice service =new Userservice();
        service.regist(user);
        String content="<a href='http://192.168.2.5:9999/webstore/user?method=active1&activeCode="+activeCode+"'>点击链接激活邮件</a>";
        Mailutils.sendMail(user.getEmail(),"激活邮件",content);
        request.getRequestDispatcher("RegistSuccess.jsp").forward(request,response);
    }
    public void active1(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String activeCode=request.getParameter("activeCode");
        Userservice service =new Userservice();
        boolean flag=service.checkactiveCode(activeCode);
        if (flag){
            service.updateactiveCode(activeCode);
            response.sendRedirect("login.jsp");
        }else {
            response.sendRedirect("register.jsp");
        }
    }
    public void checkusername(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String username=request.getParameter("username");
        Userservice service=new Userservice();
        boolean flag=service.checkname(username);
        System.out.println(flag);
        String jsonString="{\"flag\":"+flag+"}";
        response.getWriter().write(jsonString);
    }
}
